// 这份代码专门处理逻辑层的操作
const harmDao = require('../dao/harmDao');

const powerService = require('./powerService');

const harmService = {
    /*
     * 获取所有危机类型
     */
    async getAllHarmType() {
        let result;
        try {
            let selectResult = await harmDao.getAllHarmType();
            result = { success: true, msg: "查询成功", harmTypeList: selectResult }
        } catch (error) {
            result = { success: false, msg: "查询数据库时出错，查询失败" }
        }
        return result;
    },
    /*
     * 新增危机档案接口
     */
    async addHarmRecord(uid, userType, data) {
        // 用户权限认证
        let result;
        try {
            result = await powerService.checkPowerByUid(uid, '新增危机档案');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            let insertResult = await harmDao.addHarmRecord(data);
            result = { success: true, msg: "插入成功", insertId: insertResult.insertId };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，插入失败" };
        }
        return result;
    },
    /*
     * 查询某个学生的所有危机档案（暂时不做细节权限限制）
     */
    async getHarmRecordByStudent(uid, userType, studentId) {
        // 用户权限认证
        let result;
        try {
            result = await powerService.checkPowerByUid(uid, '查看学生信息');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            let insertResult = await harmDao.getHarmRecordByStudent(uid, studentId);
            result = { success: true, msg: "查询成功", result: insertResult };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，查询失败" };
        }
        return result;
    },
};

module.exports = harmService;